<template>
	<view class="setting-container" :style="{height: `${windowHeight}px`}">
		<view class="menu-list">
			<view class="list-cell list-cell-arrow" @click="handleToPwd">
				<view class="menu-item-box">
					<view class="iconfont icon-password menu-icon"></view>
					<view>修改密码</view>
				</view>
			</view>
			<view class="list-cell list-cell-arrow" @click="handleToUpgrade">
				<view class="menu-item-box">
					<view class="iconfont icon-refresh menu-icon"></view>
					<view>检查更新</view>
					<view>
						<uni-badge v-show="needUpdateFlag" :is-dot="true" text="更新" absolute="" size="normal">
						</uni-badge>
					</view>
					<view style="margin: auto;margin-right:15px"> {{appVersion}} </view>
				</view>
			</view>
			<view class="list-cell list-cell-arrow" @click="handleCleanTmp">
				<view class="menu-item-box">
					<view class="iconfont icon-clean menu-icon"></view>
					<view>清理缓存</view>
				</view>
			</view>
		</view>
		<view class="cu-list menu">
			<view class="cu-item item-box">
				<view class="content text-center" @click="handleLogout">
					<text class="text-black">退出登录</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import config from '@/config'
	import checkUpdate from '@/components/uni-upgrade-center-app/utils/check-update.js';
	import {
		getLatestVersion
	} from "@/api/eye/appUpdate.js"
	export default {
		data() {
			return {
				// 是否需要更新
				needUpdateFlag: false,
				appVersion: '',
				windowHeight: uni.getSystemInfoSync().windowHeight
			}
		},
		onLoad() {
			console.log('我的onLoad')
			this.checkNeedUpdate()
		},
		methods: {
			checkNeedUpdate() {
				let defaultAppid = '__UNI__AF0C14D'
				console.log("默认appid: ", defaultAppid);
				getLatestVersion(defaultAppid).then((res) => {
					let t = this
					t.defaultAppid = defaultAppid
					// #ifdef APP-PLUS
					plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
						console.log('获取系统appid:' + appid + ", version:" + widgetInfo.version +
							", versionCode:" + widgetInfo.versionCode)
						t.appVersion = widgetInfo.version + '_' + widgetInfo.versionCode

						let appid = plus.runtime.appid
						let wgtVersionCode = widgetInfo.versionCode

						if (appid == 'HBuilder') {
							console.log('设置appid为默认appid:' + t.defaultAppid)
							appid = t.defaultAppid
						}
						console.log('获取后台最新的版本信息:')
						console.log(res)
						if (wgtVersionCode < res.data.autoIncrementVersionNumber) {
							console.log('当前版本小于最新版')
							t.needUpdateFlag = true
						} else {
							console.log('版本已是最新的!')
						}
					})
					// #endif
				}).catch(err => {
					console.log('获取版本出错!')
					console.log(err)
				})
			},
			handleToPwd() {
				this.$tab.navigateTo('/pages/mine/pwd/index')
			},
			handleToUpgrade() {
				console.log('点击检查更新')
				checkUpdate()
			},
			handleCleanTmp() {
				this.$modal.showToast('模块建设中~')
			},
			handleLogout() {
				this.$modal.confirm('确定注销并退出系统吗？').then(() => {
					this.$store.dispatch('LogOut').then(() => {
						this.$tab.reLaunch('/pages/index')
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		background-color: #f8f8f8;
	}

	.item-box {
		background-color: #FFFFFF;
		margin: 30rpx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 10rpx;
		border-radius: 8rpx;
		color: #303133;
		font-size: 32rpx;
	}
</style>
